package com.ruoyi.h5.eval.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dgy.eval.domain.TeachEvalItem;
import com.dgy.eval.domain.TeachEvalRecord;
import com.dgy.eval.domain.TeachEvalSummary;
import com.dgy.eval.service.ITeachEvalItemService;
import com.dgy.eval.service.ITeachEvalRecordService;
import com.dgy.eval.service.ITeachEvalSummaryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.h5.eval.pojo.dto.InsertScoreDTO;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import java.math.BigDecimal;
import java.util.stream.Collectors;

/**
 * 教学评价记录Controller
 * 
 * @author ccc
 * @date 2025-02-12
 */
@RestController
@RequestMapping("h5/eval/record")
public class H5EvalRecordController extends BaseController
{
    @Autowired
    private ITeachEvalRecordService teachEvalRecordService;

    @Autowired
    private ITeachEvalItemService teachEvalItemService;

    @Autowired
    private ITeachEvalSummaryService iTeachEvalSummaryService;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 新增教学评价
     */
//    @PreAuthorize("@ss.hasPermi('h5:eval:record:add')")
    @PostMapping("/addRecord")
    public AjaxResult addRecord(@Validated @RequestBody TeachEvalRecord teachEvalRecord)
    {
        // 获取当前登陆用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 查询该用户是否对本次课程进行了评价
        TeachEvalRecord seachEvalRecord = new TeachEvalRecord();
        seachEvalRecord.setCreateBy(loginUser.getUsername());
        seachEvalRecord.setTeachRecordId(teachEvalRecord.getTeachRecordId());
        seachEvalRecord.setStatus(1);
        List<TeachEvalRecord> teachEvalRecordList = teachEvalRecordService.selectTeachEvalRecordList(seachEvalRecord);
        if(CollUtil.isNotEmpty(teachEvalRecordList)){
            return error("您已完成了对该节课进行了评价，请勿重复评价");
        }
        teachEvalRecord.setCreateBy(loginUser.getUser().getNickName());
        teachEvalRecord.setUserDepartment(user.getDept().getDeptName());
        // 根据教师工号，获取教师归属部门ID
        if(StringUtils.isNotEmpty(teachEvalRecord.getTeacherCode())){
            SysUser teacherInfo =  sysUserMapper.selectUserByUserName(teachEvalRecord.getTeacherCode());
            teachEvalRecord.setTeacherDepartmentId(teacherInfo.getDeptId());
            if(ObjectUtil.isNotNull(teacherInfo.getDept())){
                teachEvalRecord.setTeacherDepartment(teacherInfo.getDept().getDeptName());
            }
        }
        //  评价记录入库
        teachEvalRecordService.insertTeachEvalRecord(teachEvalRecord);
        // 返回评价记录ID
        return success(teachEvalRecord.getRecordId());
    }

    /**
     * 课堂表现打分
     */
//    @PreAuthorize("@ss.hasPermi('eval:record:insertScore')")
    @PostMapping("/insertScore")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addScore(@Validated @RequestBody InsertScoreDTO requestData)
    {
        // 组装完整信息
        List<TeachEvalItem> teachEvalItemList  = requestData.getAnswerList().stream().map(item -> {
                   item.setRecordId(requestData.getRecordId());
                   item.setCreateTime(DateUtils.getNowDate());
                    return item;
                }).collect(Collectors.toList());
        // 评分入库
        teachEvalItemService.insertTeachEvalItemBatch(teachEvalItemList);

        // 更新记录表中的总评分以及等级
        TeachEvalRecord teachEvalRecord = new TeachEvalRecord();
        teachEvalRecord.setRecordId(requestData.getRecordId());
        teachEvalRecord.setScore(requestData.getScore());
        teachEvalRecord.setScoreLevel(requestData.getScoreLevel());
        teachEvalRecordService.updateTeachEvalRecord(teachEvalRecord);
        return success();
    }

    /**
     * 课堂表现总结
     */
//    @PreAuthorize("@ss.hasPermi('eval:record:addSummary')")
    @PostMapping(value = "/addSummary")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addSummary(@Validated @RequestBody TeachEvalSummary teachEvalSummary)
    {
        // 总体评价入库
        iTeachEvalSummaryService.insertTeachEvalSummary(teachEvalSummary);
        // 更新评价记录状态为已完成
        TeachEvalRecord teachEvalRecord = new TeachEvalRecord();
        teachEvalRecord.setRecordId(teachEvalSummary.getRecordId());
        teachEvalRecord.setStatus(1);
        teachEvalRecordService.updateTeachEvalRecord(teachEvalRecord);
        return success( );
    }

}
